package com.bops.web.controller;

import java.util.List;
import java.util.Map;

import org.springframework.validation.Errors;



import com.bops.biz.permission.GrantManager;
import com.bops.dal.product.dataobject.SupplierDO;
import com.bops.dal.product.queryobject.SupplierQuery;
import com.bops.service.CommonServiceLocator;
import com.bops.service.SupplierService;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.Forward;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;

public class SupplierController extends AbstractController{

	private SupplierService supplierService = CommonServiceLocator.getSupplierService();
	
	 @ControllerAction
	    public WebResult deleteSupplier(Map<String, Object> model, Integer id) {
	        if (id == null) {
	            model.put("errorStatus", "编号是" + id + "记录是错误的！");
	            return null;
	        }
	        supplierService.delete(id);
	        model.put("actionStatus", id + "已经被删除成功!");
	        return searchSupplierForm(model, null);
	    }

	 @ControllerAction
	    public WebResult viewSupplierForm(Map<String, Object> model, Integer id, String action) {
		 SupplierDO supplierDO = new SupplierDO();
	            model.put("action", action);
	            if (id == null) {
	                model.put("errorStatus", "编号是" + id + "记录是错误的!");
	                return null;
	            }
	            supplierDO = supplierService.getById(id);
	            if (supplierDO == null) {
	                model.put("errorStatus", "没有编号是" + id + "的记录!");
	                return null;
	            }
	        model.put("supplierDO", supplierDO);
	        return new View("/supplier/viewSupplier.htm");
	    }
	
	
	@ControllerAction
	public WebResult searchSupplierForm(Map<String, Object> model, SupplierQuery querySupplier){
		if (GrantManager.hasPermission("supplier_add_role"))
        {
            model.put("ADD_SUPPLIER","true");
        }

        if (querySupplier == null) {
        	querySupplier = new SupplierQuery();
        	querySupplier.setNowPageIndex(0);
        }
        List<SupplierDO> supplierList = supplierService.listSupplier(querySupplier);
        model.put("supplierDO", querySupplier);
        if (supplierList != null && supplierList.size() > 0) {
            model.put("supplierList", supplierList);
        }
        model.put("pagniation", supplierList);
        
        return new View("/supplier/searchSupplier.htm");
		
	}
	
	 @ControllerAction
	    public WebResult editSupplierForm(Map<String, Object> model, SupplierDO editSupplier,
	                                             String action,Errors e) {
	        
	    	String name =editSupplier.getCompany();
	    	if (name!=null&&!"".equals(name)) {
	    		int count = supplierService.checkSupplier(editSupplier);
	    		if(count>0){//名称不能重复
	    			model.put("nameMessage", "已经存在相同的供应商，请修改！ 公司="+name);
	    			model.put("supplierDO",editSupplier);
	    			model.put("action", action);
	    	        return new View("/supplier/editSupplier.htm");
	    		}
	    	}
	        if ("add".equals(action)) {
	        	supplierService.insert(editSupplier);
	            model.put("actionStatus", "添加成功!请继续添加!");
	            return new Forward("/supplier/editSupplier.htm");
	        } else {
	        	supplierService.update(editSupplier);
	            model.put("actionStatus", "修改成功!");
	            return new Forward("/supplier/editSupplier.htm?action=" + action + "&id="
	                               + editSupplier.getId());
	        }
	    }
	
	
	    @ControllerAction
	    public WebResult searchSupplier(Map<String, Object> model) {
	        model.put("notShowRecord", true);
	        return new View("/supplier/searchSupplier.htm");
	    }
	    
	    @ControllerAction
	    public WebResult editSupplier(Map<String, Object> model, Integer id, String action) {
	      	SupplierDO supplierDO = new SupplierDO();
	        if ("edit".equals(action) || "view".equals(action)) {
	            model.put("action", action);
	            if (id == null) {
	                model.put("errorStatus", "编号是" + id + "记录是错误的!");
	                return null;
	            }
	            supplierDO = supplierService.getById(id);
	            if (supplierDO == null) {
	                model.put("errorStatus", "没有编号是" + id + "的记录!");
	                return null;
	            }
	        } else {
	            model.put("action", "add");
	        }
	        model.put("supplierList", supplierService.listSupplier(null));
	        model.put("supplierDO", supplierDO);
	        return new View("/supplier/editSupplier.htm");
	    }
	    
	    
}
